package com.gusha.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysAdminDao;
import com.gusha.dao.SysLogOperateDao;
import com.gusha.dao.UserDao;
import com.gusha.model.SysAdmin;
import com.gusha.model.SysLogOperate;
import com.gusha.model.SysMenu;
import com.gusha.model.SysRole;
import com.gusha.model.User;
import com.gusha.service.SysMenuService;
import com.gusha.utils.Constants;
import com.gusha.utils.DateUtils;
import com.gusha.utils.IpUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.Md5Util;
import com.gusha.utils.MobileUtil;


/**
 * @Description: 登陆，退出，首页等
 * @Athor: 龙爷
 * @Company:
 * @Date: 2018-05-16 14:09:48
 */
@CrossOrigin(origins="*",maxAge=3600)
@Controller
public class IndexController {
	
	@Autowired
	private SysAdminDao sysAdminDao;
	@Autowired
	private SysLogOperateDao sysLogOperateDao;
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private UserDao userDao;

	
	
	@RequestMapping("/")
	public String toLogin(Model model, HttpServletRequest request) {
		return "/admin/common/login";
	}
	
	
	
	// 登录
	@PostMapping("/doLogin")
	@ResponseBody
	public JsonResult doLogin(String userName, String password, HttpSession session, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult(false);
		SysAdmin sysAdmin = sysAdminDao.findByUserNameAndPassword(userName, Md5Util.getMD5Code(password));
		
		if (sysAdmin == null) {
			jsonResult.setMsg("用户名或者密码错误！");
			return jsonResult;
		}
		if ("1".equals(String.valueOf(sysAdmin.getIsDelete()))) {
			jsonResult.setMsg("该帐号已删除！");
			return jsonResult;
		}

		session.setAttribute("admin", sysAdmin);
		sysAdmin.setLastLoginIp(IpUtils.getIpAddr(request));
		sysAdmin.setLastLoginTime(DateUtils.getNow());
		sysAdminDao.saveAndFlush(sysAdmin);
			
		SysRole sysRole = sysAdmin.getRole();
		session.setAttribute("role", sysRole);
		
		session.setAttribute("imagePath", Constants.IMAGE_PATH);
		
		SysLogOperate log = new SysLogOperate();
		log.setSysAdmin(sysAdmin);
		log.setCreateTime(DateUtils.getNow());
		log.setIp(IpUtils.getIpAddr(request));
		log.setTitle("登录系统");
		log.setComplete("执行成功！");
		sysLogOperateDao.saveAndFlush(log);
		
		jsonResult.setFlag(true);
	
		return jsonResult;
	}
	//用户登录
	@PostMapping("/doLoginByUser")
	@ResponseBody
	public JsonResult doLoginByUser(String userName, String password, HttpSession session, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult(false);
		User user = userDao.findByPhoneAndPassword(userName, Md5Util.getMD5Code(password));
		if (user == null) {
			jsonResult.setMsg("用户名或者密码错误！");
			return jsonResult;
		}
		session.setAttribute("user", user);
		userDao.saveAndFlush(user);
		session.setAttribute("imagePath", Constants.IMAGE_PATH);
		jsonResult.setObj(user);
		jsonResult.setMsg("登录成功");
		jsonResult.setFlag(true);
		return jsonResult;
		
	}
	
	//用户注册
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/doRegistered")
	@ResponseBody
	public JsonResult doRegistered(String userName, String password,HttpSession session, HttpServletRequest request,String usertel) {
		
		JsonResult jsonResult = new JsonResult(false);
		User u=userDao.findByPhone(usertel);
		if(u!=null) {
			jsonResult.setMsg("您的手机号码已注册过");
			return jsonResult;
		}
		User user=new User();
		user.setPhone(usertel);
		user.setUserType(0);
		user.setUserName(usertel);
		user.setPassword(Md5Util.getMD5Code(password));
		userDao.saveAndFlush(user);
		
		session.setAttribute("userName",userName );
		session.setAttribute("password",password );
		jsonResult.setFlag(true);
		jsonResult.setMsg("注册成功");
		return jsonResult;
		
	}
	//忘记密码
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/updatePwd")
	@ResponseBody
	public JsonResult updatePwd(String userName,String password,HttpSession session,HttpServletRequest request,String usertel) {
		
		JsonResult jsonResult = new JsonResult(false);
		User u=userDao.findByPhone(usertel);
		if(u.getPhone()==null) {
			u.setPassword(Md5Util.getMD5Code(password));
			userDao.saveAndFlush(u);
			session.setAttribute("userName",userName );
			session.setAttribute("password",password );
			jsonResult.setFlag(true);
			jsonResult.setMsg("修改成功");
		}else {
			
			jsonResult.setMsg("手机号码不一致");
		}
	
		
		return jsonResult;
		
	}
	

	
	/**
	 * @Description: 修改密码
	 * @Athor: 龙爷
	 * @Company:
	 * @Date: 2018-05-21 10:56:01
	 */
	@PostMapping("doUpdatePass")
	@ResponseBody
	public JsonResult doUpdatePass(Model model, HttpServletRequest request, String oldPass,String newPass) {
		JsonResult jsonResult = new JsonResult(false);
		SysAdmin admin = (SysAdmin) request.getSession().getAttribute("admin");
		if(!Md5Util.getMD5Code(oldPass).equals(admin.getPassword())) {
			jsonResult.setMsg("原密码不正确");
			return jsonResult;
		}
		admin.setPassword(Md5Util.getMD5Code(newPass));
		admin = sysAdminDao.saveAndFlush(admin);
		request.getSession().invalidate();
		jsonResult.setFlag(true);
		jsonResult.setMsg("密码修改成功，请用新密码登陆");
		return jsonResult;
	}
	
	@RequestMapping("/gx/main")
	public String main(Model model,HttpServletRequest request, HttpSession session) {
		// 获取一级二级
		List<SysMenu> listMenus = sysMenuService.getOneAndTwoMenuListByRole(session);
		model.addAttribute("listMenus", listMenus);
		return "/admin/common/main";
	}
	
	@RequestMapping("/loginOut")
	@SysOperateLog(operate = "退出系统")
	public String loginout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "redirect:/";
	}

	@RequestMapping("/to403")
	public String to403(Model model) {
		return "/admin/error/403";
	}

	@RequestMapping("/to500")
	public String to500(Model model) {
		return "/admin/error/500";
	}

	@RequestMapping("/toAjax500")
	@ResponseBody
	public JsonResult toAjax500(HttpServletRequest request) {
		JsonResult result = new JsonResult(false);
		result.setMsg("系统内部错误");
		return result;
	}

	@RequestMapping("/to404")
	public String to404(Model model) {
		return "/admin/error/404";
	}
}
